package es.armaplus.commons.tags;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ArmaplusTagBase extends SimpleTagSupport {

	protected String label;
	protected String value;
	private String styleClass;
	private String tagContent = DEFAULT_TAG_CONTENT;
	private String tagLabel = DEFAULT_TAG_LABEL;
	private String tagValue = DEFAULT_TAG_VALUE;
	
	protected static final String DEFAULT_STYLE = "campo";
	protected static final String DEFAULT_TAG_VALUE = "span";
	protected static final String DEFAULT_TAG_LABEL= "label";
	protected static final String DEFAULT_TAG_CONTENT= "div";
	protected static final String OPEN_TAG_BEGIN = "<";
	protected static final String OPEN_TAG_END = "</";
	protected static final String CLOSE_TAG = ">";
	
	
	
	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getStyleClass() {
		String style = styleClass;
		if(styleClass==null || styleClass.equals("")){
			style = DEFAULT_STYLE;
		}
		return style;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * Este método nos permite generar el cuerpo de la etiqueta
	 * desde unas primitivas preestablecidas.
	 * 
	 * @return
	 */
	protected String generateBody(){
		String body = null;
		Map<String,String> mapContent = new HashMap<String, String>();
		mapContent.put("class", getStyleClass());
		
		body = generateTag(mapContent, tagContent,
				generateTag(null, tagLabel, label) +
				generateTag(null, tagValue, value)
				);
		return body;
	}
	
	protected String generateTag(Map<String,String> propertyValue, 
			String tagName, 
			String tagBody){
		String tag = null;
		
		tag = OPEN_TAG_BEGIN + tagName + getProperties(propertyValue) + 
			CLOSE_TAG + tagBody + OPEN_TAG_END + tagName + CLOSE_TAG;
		
		return tag;
		
	}
	
	private String getProperties(Map <String, String> propertyValue){
		String properties = new String();
		if(propertyValue!=null){
			for(Iterator it = propertyValue.keySet().iterator(); it.hasNext();){
				String property = (String) it.next();
				String value = propertyValue.get(property);
				properties += " " + property + "=\"" + value + "\"";
			}
			
		}
		return properties; 
	}

	public String getTagContent() {
		return tagContent;
	}

	public void setTagContent(String tagContent) {
		this.tagContent = tagContent;
	}

	public String getTagLabel() {
		return tagLabel;
	}

	public void setTagLabel(String tagLabel) {
		this.tagLabel = tagLabel;
	}

	public String getTagValue() {
		return tagValue;
	}

	public void setTagValue(String tagValue) {
		this.tagValue = tagValue;
	}
}
